package xyz.icxl.android.architecture.ui

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.LanguageUtils
import com.blankj.utilcode.util.ToastUtils
import xyz.icxl.android.architecture.R
import xyz.icxl.android.architecture.dialog.LoadingDialog
import xyz.icxl.android.architecture.observer.PageLoadingStatusObserver
import xyz.icxl.android.architecture.observer.PageSnackBarStatusObserver
import xyz.icxl.android.architecture.receiver.TokenMissHelper

abstract class BaseActivity<DB : ViewDataBinding> : AppCompatActivity() {

    open val title = ""

    /**
     * 是否有Token限制
     */
    protected open val tokenRestrictions = true
    protected lateinit var dataBinding: DB
    abstract val layout: Int
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setNavBarColor(this, Color.parseColor("#F6F6F6"))
        BarUtils.setNavBarLightMode(this, true)
        dataBinding = DataBindingUtil.setContentView(this, layout)
        dataBinding.lifecycleOwner = this
        dataBinding.root.findViewById<TextView>(R.id.tv_title)?.text = title
        dataBinding.root.findViewById<View>(R.id.ib_back)?.setOnClickListener { onBackPressed() }
        initView()
        initData()
        if (tokenRestrictions) {
            TokenMissHelper.getInstance().observe(this){
                finish()
            }
        }
    }

    abstract fun initView()
    abstract fun initData()

    protected val pageLoadingDialogStatus by lazy { PageLoadingStatusObserver(supportFragmentManager) }
    protected val pageSnackbarStatusObserver by lazy { PageSnackBarStatusObserver(dataBinding.root) }

    override fun onDestroy() {
        if (pageLoadingDialogStatus.loadingDialog?.isShowing == true && pageLoadingDialogStatus.loadingDialog?.parentFragment?.isStateSaved == false)
            pageLoadingDialogStatus.loadingDialog?.dismiss()
        pageSnackbarStatusObserver.dismiss()
        if (loadingDialog?.isShowing == true)
            loadingDialog?.dismiss()
        super.onDestroy()
    }

    private var loadingDialog: LoadingDialog? = null
    fun showLoading(msg: String = "loading···",isCancelAble:Boolean = true) {
        if (null == loadingDialog) {
            loadingDialog = LoadingDialog(msg,isCancelAble)
        }
        if (loadingDialog?.isShowing == false) {
            loadingDialog?.show(supportFragmentManager)
        }
    }

    open fun loadingFail(msg: String = "loading fail.") {
        loadingDialog?.dismiss()
        ToastUtils.showShort(msg)
    }

    open fun dismissLoadingDialog() {
        loadingDialog?.dismiss()
    }

    open fun loadingSuccess(msg: String? = null) {
        loadingDialog?.dismiss()
        if (!msg.isNullOrEmpty())
            ToastUtils.showShort(msg)
    }

    override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(LanguageUtils.attachBaseContext(newBase))
    }
}